package util

import java.io._

/**
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: 27.2.11
 * Time: 10:57
 * To change this template use File | Settings | File Templates.
 */

object StreamUtils {
  def copy(input: InputStream, output: OutputStream) {
    val buffer = new Array[Byte](1024)
    var n = 0
    do {
      n = input.read(buffer, 0, buffer.length)
      if (n > 0)
        output.write(buffer, 0, n)
    } while (n >= 0)
  }

  def readAsString(input: InputStream, charset: String): String = {
    val bos = new ByteArrayOutputStream
    copy(input, bos)
    bos.close
    new String(bos.toByteArray, charset)
  }

  def readLine(input: InputStream): String = {
    val reader = new BufferedReader(new InputStreamReader(input))
    reader.readLine
  }

}